package com.isoft.mapper;

import com.isoft.entity.DfoeDfieEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfoeDfieMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) VALUES(#{entity.meta.sender}, #{entity.meta.receiver}, #{entity.meta.sequenceNumber}, TO_TIMESTAMP(#{entity.meta.dateTime}, 'YYYYMMDDHH24MISS'), #{entity.meta.type}, #{entity.meta.subType})")
    void insertMeta(@Param("entity") DfoeDfieEntity entity);

    @Insert("INSERT INTO FLIGHT_BASE(" +
            "FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, " +
            "STATUS, INTERNAL_STATUS, MANUAL_STATUS, MANUAL_INTERNAL_STATUS, RELEASE_TIME) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "COALESCE(NULLIF(#{dflt.status,jdbcType=VARCHAR},''), 'NORMAL'), " +
            "#{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalStatus,jdbcType=VARCHAR}, " +
            "TO_TIMESTAMP(#{dflt.flightExecutionDate,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))")
    int insertFlightBase(DfoeDfieEntity entity);

    @Insert("INSERT INTO AIRCRAFT(AIRCRAFT_ID, FLIGHT_ID, AIRCRAFT_TYPE, AIRCRAFT_NUMBER) " +
            "VALUES(SEQ_AIRCRAFT.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.aircraftType,jdbcType=VARCHAR}, " +
            "#{dflt.aircraftNumber,jdbcType=VARCHAR})")
    int insertAircraft(DfoeDfieEntity entity);

    @Insert("INSERT INTO SPECIAL_PASSENGER(PASSENGER_ID, FLIGHT_ID, VIP_COUNT) " +
            "VALUES(SEQ_PASSENGER.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "COALESCE(#{dflt.vipCount,jdbcType=NUMERIC}, 0))")
    int insertSpecialPassenger(DfoeDfieEntity entity);

    @Insert("INSERT INTO FLIGHT_IRREGULARITY(" +
            "FLIGHT_ID, ABNORMAL_STATUS, ABNORMAL_REASON, " +
            "INTERNAL_STATUS, INTERNAL_REASON, MANUAL_ABNORMAL_STATUS, " +
            "MANUAL_ABNORMAL_REASON, MANUAL_INTERNAL_STATUS, MANUAL_INTERNAL_REASON) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.abnormalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.abnormalReason,jdbcType=VARCHAR}, " +
            "#{dflt.internalAbnormalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.internalAbnormalReason,jdbcType=VARCHAR}, " +
            "#{dflt.manualAbnormalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualAbnormalReason,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalAbnormalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalAbnormalReason,jdbcType=VARCHAR})")
    int insertFlightIrregularity(DfoeDfieEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO FLIGHT_ROUTE(" +
                    "ROUTE_ID, FLIGHT_ID, AIRPORT_CODE, AIRPORT_SEQ, " +
                    "SCHEDULED_TIME, ESTIMATED_TIME, ACTUAL_TIME, AIRPORT_TERMINAL) " +
                    "VALUES ",
            "<foreach collection='dflt.airl.arpts' item='arpt' separator=','>",
            "(SEQ_ROUTE.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{arpt.airportCode,jdbcType=VARCHAR}, ",
            "#{arpt.airportSeq,jdbcType=NUMERIC}, ",
            "TO_TIMESTAMP(#{arpt.scheduledTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{arpt.estimatedTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{arpt.actualTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "#{arpt.airportTerminal,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    int insertFlightRoute(DfoeDfieEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO CHECKIN_COUNTER(COUNTER_ID, FLIGHT_ID, COUNTER_NUMBER, START_TIME, END_TIME, COUNTER_TYPE) ",
            "VALUES ",
            "<if test='dflt.ckls.fces != null'>",
            "(SEQ_COUNTER.nextval, #{dflt.flightId}, #{dflt.ckls.fces}, ",
            "TO_TIMESTAMP(#{dflt.ckls.fces,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{dflt.ckls.fcee,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), 'F'),",
            "</if>",
            "<if test='dflt.ckls.mces != null'>",
            "(SEQ_COUNTER.nextval, #{dflt.flightId}, #{dflt.ckls.mces}, ",
            "TO_TIMESTAMP(#{dflt.ckls.mces,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{dflt.ckls.mcee,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), 'M')",
            "</if>",
            "</script>"
    })
    int insertCheckinCounter(DfoeDfieEntity entity);

    @Insert("INSERT INTO GATE(GATE_ID, FLIGHT_ID, GATE_NUMBER, GATE_TYPE) " +
            "VALUES(SEQ_GATE.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.tmcd.gateNumber,jdbcType=VARCHAR}, " +
            "'T2')")
    int insertGate(DfoeDfieEntity entity);

    @Insert("INSERT INTO BAGGAGE_INFO(BAGGAGE_ID, FLIGHT_ID, FIRST_BAG_TIME, LAST_BAG_TIME) " +
            "VALUES(SEQ_BAGGAGE.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{dflt.fldt,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "TO_TIMESTAMP(#{dflt.lldt,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))")
    int insertBaggageInfo(DfoeDfieEntity entity);

    @Insert("INSERT INTO BOARDING_TIME(TIME_ID, FLIGHT_ID, BOARDING_TYPE, BOARDING_TIME) " +
            "VALUES(SEQ_BOARDING.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "'BORT', " +
            "TO_TIMESTAMP(#{dflt.boardingTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))")
    int insertBoardingTime(DfoeDfieEntity entity);
}